//
//  ShoppingCartTableViewCell.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/8/2.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit

typealias BackBoolValue = (Bool)->Void

class ShoppingCartTableViewCell: UITableViewCell {

    @IBOutlet weak var goodsImage: UIImageView!
    @IBOutlet weak var xiajiaView: UIView!
    @IBOutlet weak var jianButton: UIButton!
    @IBOutlet weak var countLabel: UILabel!
    @IBOutlet weak var jiaButton: UIButton!
    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var price: UILabel!
    @IBOutlet weak var marketPrice: UILabel!
    @IBOutlet weak var subtotalLabel: UILabel!
    @IBOutlet weak var seleBtn: UIButton!
    
    var goodsCount = 1
    
    var onePrice:Double = 0
    
    var seleClosure:BackBoolValue!
    
    var backGoodsCount:BackIntValue!
    
    var model:SiteGoodsModel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.xiajiaView.backgroundColor = UIColor.init(hexColor: "000000",alpha:0.6)
        self.xiajiaView.isHidden = true
        self.goodsImage.corner(byRoundingCorners:.allCorners, radii: 10)
        self.xiajiaView.corner(byRoundingCorners: .allCorners, radii: 10)
        
    }
    
    
    // MARK: 修改删除按钮样式 iOS 11 之前
    override func layoutSubviews() {
        super.layoutSubviews()
        
        for subView in self.subviews {
            if subView.isKind(of: NSClassFromString("UITableViewCellDeleteConfirmationView")!){
                for btn in subView.subviews {
                    if btn.isKind(of: UIButton.classForCoder()) {
                        let button = btn as! UIButton
                        button.setTitle("", for: .normal)
                        button.backgroundColor = UIColor.init(hexColor: "#000000")
                        button.setImage(UIImage.init(named: "icon_delete_3"), for: .normal)
                    }
                }
            }
        }  
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func reload(withModel model:SiteGoodsModel){
        
        self.model = model
        self.goodsImage.my_setImage(url: model.img, placeholder: Api.placeHolder.rawValue)
        self.name.text = model.name
        
        var currentPrice:Double = 0
        if model.isDiscount == 1 {
            currentPrice = model.discountMoney
        }else if model.isActivity == 1 {
            currentPrice = model.activityMoney
        }else if model.peddlersPrice != nil {
            currentPrice = Double.init((model.peddlersPrice)!)!
        }else{
            currentPrice = model.price
        }
        self.onePrice = currentPrice
        
        let str = "￥" + "\(currentPrice)" + "/" + model.spec + "  " + model.specDesc
        let attribute = NSMutableAttributedString.init(string: str)
        attribute.addAttributes([NSAttributedStringKey.font:UIFont.systemFont(ofSize: 11)], range: NSString.init(string: str).range(of: model.specDesc))
        self.price.attributedText = attribute
        self.marketPrice.attributedText = Utils.labelTextDeleLine(normalString: "市场价：¥\(model.marketPrice)", deleString: "\(model.marketPrice)")
        self.updateTotalPrice()
        if model.state == 0 {
            self.normalStyle()
        }else{
            self.soldOutStyle()
        }
    }
    
    func updateTotalPrice(){
        
        var attributeString = NSMutableAttributedString.init(string: "小计：¥" + "\(self.onePrice * Double(self.goodsCount))")
        attributeString.addAttributes([NSAttributedStringKey.font:UIFont.systemFont(ofSize: 16)], range: NSString.init(string: "小计：¥" + "\(self.onePrice * Double(self.goodsCount))").range(of: "\(self.onePrice * Double(self.goodsCount))".getPriceInteger()))
        attributeString = NSMutableAttributedString.init(attributedString: attributeString)
        attributeString.addAttributes([NSAttributedStringKey.foregroundColor:UIColor.init(hexColor: "#ed4848")], range: NSString.init(string: "小计：¥" + "\(self.onePrice * Double(self.goodsCount))").range(of: "¥" + "\(self.onePrice * Double(self.goodsCount))"))
        self.subtotalLabel.attributedText = attributeString
    }
    
    @IBAction func jianMethod(_ sender: UIButton) {
        guard self.goodsCount >= 2 else {
            return
        }
        self.goodsCount = self.goodsCount - 1
        self.countLabel.text = "\(self.goodsCount)"
        self.backGoodsCount(self.goodsCount)
        self.updateTotalPrice()
    }
    
    @IBAction func jiaMethod(_ sender: UIButton) {
        self.goodsCount = self.goodsCount + 1
        self.countLabel.text = "\(self.goodsCount)"
        self.backGoodsCount(self.goodsCount)
        self.updateTotalPrice()
    }
    
    
    @IBAction func seleBtnClick(_ sender: UIButton) {
        
        if self.model.state != 0 {
            PKhud.showText(text: "该商品已下架~")
            return
        }
        
        if sender.isSelected {
            if self.seleClosure != nil {
                self.seleClosure(false)
            }
        }else{
            if self.seleClosure != nil {
                self.seleClosure(true)
            }
        }
        
        sender.isSelected = !sender.isSelected
    }
    
    func soldOutStyle(){
        self.xiajiaView.isHidden = false
        self.jianButton.setTitleColor(UIColor.init(hexColor: "#cccccc"), for: .normal)
        self.jianButton.isUserInteractionEnabled = false
        self.jiaButton.setTitleColor(UIColor.init(hexColor: "#cccccc"), for: .normal)
        self.jiaButton.isUserInteractionEnabled = false
        self.countLabel.textColor = UIColor.init(hexColor: "#cccccc")
    }
    
    func normalStyle(){
        self.xiajiaView.isHidden = true
        self.jianButton.setTitleColor(UIColor.init(hexColor: "#000000"), for: .normal)
        self.jianButton.isUserInteractionEnabled = true
        self.jiaButton.setTitleColor(UIColor.init(hexColor: "#000000"), for: .normal)
        self.jiaButton.isUserInteractionEnabled = true
        self.countLabel.textColor = UIColor.init(hexColor: "#000000")
    }
}
